package sliding_window;

public class minOperations {
    public int minOperations(int[] nums, int x) {
        int n = nums.length;
        int left = 0;
        int right = 0;
        int len = -1;
        int sum = 0;
        for (int a : nums) {
            sum += a;
        }
        int target = sum - x;
        if(target < 0) {
            return len;
        }
        sum = 0;
        while (right < n) {
            sum += nums[right];
            while (sum > target) {
                sum -= nums[left];
                left++;
            }
            if (sum == target) {
                len = Math.max(right - left + 1, len);
            }
            right++;
        }
        return len == -1 ? len: n - len;

    }
}
